package com.shigu.admin.action;

import com.shigu.MallResponse;
import com.shigu.PageResponse;
import com.shigu.advert.bo.ModifyAdvertRequest;
import com.shigu.exceptions.AdminException;
import com.shigu.item.bo.*;
import com.shigu.item.service.ItemAdminService;
import com.shigu.item.service.XzDaifaItemService;
import com.shigu.item.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.text.ParseException;

@Controller
@RequestMapping("adminjson")
public class ItemAdminController {
    @Autowired
    ItemAdminService itemAdminService;
    @Autowired
    XzDaifaItemService xzDaifaItemService;

    @RequestMapping("addItem")
    @ResponseBody
    public MallResponse addItem(@RequestBody AddItemRequest request) throws AdminException {

        AddItemBo bo = new AddItemBo();
        BeanUtils.copyProperties(request, bo);
//        bo.setItemCode(request.getItemCode());
//        bo.setTitle(request.getTitle());
//        bo.setPrice(Double.valueOf(request.getPrice()));
//        bo.setDiscount(Double.valueOf(request.getDiscount()));
//        bo.setImages(request.getImages());
//        bo.setDetails(request.getDetails());
//        bo.setFakeSales(request.getFakeSales());
//        bo.setSellerId(request.getSellerId());
//        bo.setSkus(request.getSkus());
//        bo.setParams(request.getParams());
//        bo.setFeeId(request.getFeeId());
//        bo.setNum(request.getNum());
//        if(StringUtils.isNotBlank(request.getCid())){
//            bo.setCid(Long.valueOf(request.getCid()));
//        }
//        if(StringUtils.isNotBlank(request.getAdvertImg())){
//            bo.setAdvertImg(request.getAdvertImg());
//        }
//        if(request.getWeight()!=null){
//            bo.setWeight(request.getWeight());
//        }

        return itemAdminService.addItem(bo);
    }

    @RequestMapping("noSaleItem")
    @ResponseBody
    public MallResponse noSaleItem(@Valid ItemIdRequest request, BindingResult result) throws AdminException {
        if (result.hasErrors()) {
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return itemAdminService.noSaleItem(request.getItemId());
    }


    @RequestMapping("modifyItem")
    @ResponseBody
    public MallResponse modifyItem(@RequestBody @Valid ModifyItemRequest request, BindingResult result) throws AdminException {
        if (result.hasErrors()) {
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        ModifyItemBo bo = new ModifyItemBo();
        BeanUtils.copyProperties(request, bo);
//        bo.setItemCode(request.getItemCode());
//        bo.setTitle(request.getTitle());
//        bo.setPrice(Double.valueOf(request.getPrice()));
//        bo.setDiscount(Double.valueOf(request.getDiscount()));
//        bo.setImages(request.getImages());
//        bo.setDetails(request.getDetails());
//        bo.setFakeSales(request.getFakeSales());
//        bo.setSellerId(request.getSellerId());
//        bo.setSkus(request.getSkus());
//        bo.setParams(request.getParams());
//        bo.setFeeId(request.getFeeId());
//        bo.setItemId(request.getItemId());
//        if(StringUtils.isNotBlank(request.getAdvertImg())){
//            bo.setAdvertImg(request.getAdvertImg());
//        }
//        if(request.getWeight()!=null){
//            bo.setWeight(request.getWeight());
//        }
//        if(StringUtils.isNotBlank(request.getCid())){
//            bo.setCid(Long.valueOf(request.getCid()));
//        }
//        bo.setNum(request.getNum());
        return itemAdminService.modifyItem(bo);
    }

    @RequestMapping("selItemById")
    @ResponseBody
    public SelItemResponse selItemById(@Valid ItemIdRequest request, BindingResult result) throws AdminException {
        if (result.hasErrors()) {
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return itemAdminService.selItemById(request.getItemId());
    }

    @RequestMapping("allitemList")
    @ResponseBody
    public PageResponse<ItemListVo> allitemList(AllItemListRequest request) throws ParseException {
        return itemAdminService.allitemList(request.getPage(), request.getSize(), request.getStatus()
                , request.getItemCode(), request.getCateId()
                , request.getBefore(), request.getAfter(), request.getTitle(),
                request.getStoreNum(), request.getGoodsNo());
    }

    @RequestMapping("saleItem")
    @ResponseBody
    public MallResponse saleItem(@Valid ItemIdRequest request, BindingResult result) throws AdminException {
        if (result.hasErrors()) {
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return itemAdminService.saleItem(request.getItemId());
    }

    @RequestMapping("deleteItem")
    @ResponseBody
    public MallResponse deleteItem(@Valid ItemIdRequest request, BindingResult result) throws AdminException {
        if (result.hasErrors()) {
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return itemAdminService.deleteItem(request.getItemId());
    }

    @RequestMapping("modifyItemAdvert")
    @ResponseBody
    public MallResponse modifyItemAdvert(ModifyAdvertImgRequest request) {
        return itemAdminService.modifyItemAdvert(request.getItemId(), request.getImgUrl());
    }

    @RequestMapping("itemCodeToId")
    @ResponseBody
    public ItemIdResponse itemCodeToId(@Valid ItemCodeRequest request, BindingResult result) throws AdminException {
        if (result.hasErrors()) {
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return itemAdminService.itemCodeToId(request.getItemCode());
    }

    @RequestMapping("getShiguGoods")
    @ResponseBody
    public ShiguGoodsResponse getShiguGoods(GetShiguGoodsRequest request) throws AdminException {
        return xzDaifaItemService.getShiguGoods(request.getItemCode(), request.getWebSite());
    }

    @RequestMapping("getItemDaifaInfo")
    @ResponseBody
    public ItemDaifaResponse getItemDaifaInfo(GetShiguGoodsRequest request) throws AdminException {
        return xzDaifaItemService.getItemDaifaInfo(request.getItemCode(),request.getWebSite());
    }
}
